<template>
  <div class="login">
    <div class="box">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item>
          <div style="text-align: center">
            <img src="@/assets/common/login-logo.png" alt="">
          </div>
        </el-form-item>
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" prefix-icon="el-icon-user-solid" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-lock"
            type="password"
            @keyup.enter.native="submit"
          />
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" @click="submit">登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// import { sysLogin } from '@/api/login.js'
import router from '@/router'
// import CookieJs from 'js-cookie'
export default {
  component: { router },
  data() {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确的手机号'))
              }
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            min: 6,
            max: 12,
            message: '请输入6至12位密码',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          // sysLogin(this.form)
          //   .then((res) => {
          //     if (res.success) {
          //       console.log('成功的处理')
          //     } else {
          //       this.$message.error(res.message)
          //     }
          //     this.$message.success('登陆成功')
          //     console.log(res)
          //   })
          //   .catch((error) => {
          //     console.log('err:', error)
          //   })
          // const res = await sysLogin(this.form)
          // this.$message.success('登陆成功')
          // CookieJs.set('hr', res.data)
          // console.log(CookieJs.get('hr'))
          // // CookieJs.remove('hr') 清除
          // console.log(res)
          await this.$store.dispatch('user/toLogin', this.form)
          // 如果有回跳地址，登陆成功之后要跳转到回跳页面
          // 没有就跳回首页
          // const _redirect = this.$route.query.redirect || '/'
          // this.$router.push(_redirect)
          this.$router.push('/')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  background: url('../../assets/common/login.jpg') no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 550px;
    .el-button {
      background-color: #407ffe;
      color: #fff;
      border-width: 0;
    }
  }
}
</style>
